<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="500px"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item :label="$t('user_userName')" prop="username" :error="error.username">
        <el-input
          v-model="form.username"
          :disabled="!createFlag"
          maxlength="24"
          show-word-limit
          :placeholder="$t('inputUserName')"
        />
      </el-form-item>
      <el-form-item :label="$t('user_name')" prop="name" :error="error.name">
        <el-input
          v-model="form.name"
          maxlength="24"
          show-word-limit
          :placeholder="$t('inputName')"
        />
      </el-form-item>
      <el-form-item :label="$t('userEmail')" prop="email" :error="error.email">
        <el-input
          v-model="form.email"
          maxlength="100"
          show-word-limit
          :placeholder="$t('inputUserEmail')"
        />
      </el-form-item>
      <el-form-item :label="$t('role')">
        <el-select
          v-model="form.roles"
          multiple
          collapse-tags
          collapse-tags-tooltip
          :max-collapse-tags="5"
          style="width: 100%"
          :placeholder="$t('role')"
        >
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
            <span style="float: left">{{ item.code }}</span>
            <span style="float: right; font-size: 13px; color: var(--el-text-color-secondary)">{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-show="!createFlag" :label="$t('userIsActive')">
        <el-switch v-model="form.is_active" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" @click="submitForm">{{
        $t('create')
      }}</el-button>
      <el-button v-else type="primary" @click="submitForm">{{
        $t('update')
      }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateUserApi, createUserApi } from '@/views/rbac/api/rbac.api';
import { getDropDownRoleListApi } from '@/api/common/common';

export default {
  data() {
    return {
      title: undefined,
      openSheet: false,
      createFlag: true,
      userId: undefined,
      options: [],
      form: {
        username: undefined,
        name: undefined,
        email: undefined,
        is_active: true,
        roles: undefined
      },
      error: {
        username: undefined,
        name: undefined,
        email: undefined
      }
    };
  },
  computed: {
    rules() {
      return {
        username: [
          {
            required: true,
            message: this.$t('usernameRequire'),
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: this.$t('nameRequire'),
            trigger: 'blur'
          }
        ],
        email: [
          {
            required: true,
            message: this.$t('emailRequire'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    openSheetDialog(data) {
      this.getRoleOptions();
      this.reset();
      if (data) {
        this.title = this.$t('userUpdate');
        this.createFlag = false;
        this.userId = data.id;
        this.form = data;
      } else {
        this.title = this.$t('userCreate');
        this.createFlag = true;
      }
      this.openSheet = !this.openSheet;
    },
    cancel() {
      this.openSheet = !this.openSheet;
    },
    reset() {
      this.resetForm('form');
      this.resetJsonObject(this.form)
    },
    async getRoleOptions() {
      var resp = await getDropDownRoleListApi();
      if (resp.success) {
        this.options = resp.data;
      } else {
        this.targaMessageBox(this, resp.msg)
      }
    },
    submitForm: function() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          this.resetJsonObject(this.error)
          var resp;
          if (this.createFlag) {
            resp = await createUserApi(this.form);
          } else {
            resp = await updateUserApi(this.userId, this.form);
          }
          if (resp.success) {
            this.$emit('refreshList');
            this.cancel();
            if (this.createFlag) {
              this.targaMessage(
                this.$t('createSuccess', {
                  title: this.$t('user_userName'),
                  value: this.form.username
                })
              );
            } else {
              this.targaMessage(
                this.$t('updateSuccess', {
                  title: this.$t('user_userName'),
                  value: this.form.username
                })
              );
            }
          } else {
            if (typeof resp.msg === 'object') {
              this.error = resp.msg
            } else {
              this.targaMessageBox(this, resp.msg)
            }
          }
        }
      });
    }
  }
};
</script>

<style></style>
